package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class build_class
 */
@WebServlet("/build_class")
public class build_class extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public build_class() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		String centername = request.getParameter("centername");
		String classname = request.getParameter("classname");
		// 外教两部分的时间拼接起来
		String ftweek = request.getParameter("ftweek");
		String fttime_start = request.getParameter("fttime_start");
		String fttime_end = request.getParameter("fttime_end");
		String ftime = ftweek + fttime_start + "-" + fttime_end;
		System.out.println(ftime);
		// 中教两部分时间拼接起来
		String ctweek = request.getParameter("ctweek");
		String cttime_start = request.getParameter("cttime_start");
		String cttime_end = request.getParameter("cttime_end");
		String ctime = ctweek + cttime_start + "-" + cttime_end;
		System.out.println(ctime);

		String fteachername = request.getParameter("fteachername");
		String cteachername = request.getParameter("cteachername");
		String classlevel = request.getParameter("classlevel");
		String startdate = request.getParameter("startdate");
		String enddate = request.getParameter("enddate");
		String stunum = request.getParameter("stunum");
		String stunumrest = stunum;
		// 插入SQL语句
		String insert = "insert into class(centername,classname,fttime,cttime,fteacher,cteacher,classlevel,startdate,enddate,stunum,stunumrest)values("
				+ "'" + centername + "','" + classname + "','" + ftime + "','" + ctime + "','" + fteachername + "','"
				+ cteachername + "','" + classlevel + "','" + startdate + "','" + enddate + "','" + stunum + "','"
				+ stunumrest + "')";
		// 调用JDBC插入数据库
		//班级建立后建立考勤表
		String create = " create table "+centername+classname+"check"+"(stuid varchar(20) character set utf8 not null,stuname varchar(20) character set utf8 not null,"
				+ "classname varchar(20) character set utf8 not null, centername varchar(20) character set utf8 not null,"
				+ "total int,absence int, primary key(stuid))";
		System.out.println(create);
		Jdbc j1 = null;

		try {
			j1 = new Jdbc(insert);
			j1 = new Jdbc(create);

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		request.getRequestDispatcher("showsuccess.jsp").forward(request, response);

	}

}
